package com.jiaertangshi.jiaer.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jiaertangshi.jiaer.dto.Tuser;
import com.jiaertangshi.jiaer.dto.UserInfo;
import com.jiaertangshi.jiaer.service.UserService;

import net.sf.json.JSONObject;

/**
 * @ClassName: TuserController
 * @Description: TuserController
 * @author zfy
 * @date 2019年1月14日
 *
 */
@RequestMapping("/user")
@RestController
public class TuserController {
	
	
	@Autowired
	UserService   userService;
	

	/*
	 * 注册用户信息
	 */
	@RequestMapping(value = "/userRegister", method = RequestMethod.POST)
	public   int   userRegister(@RequestParam(value = "openid", required = true) String openid,
			@RequestParam(value = "headImg", required = true) String headImg,
			@RequestParam(value = "nickname", required = true) String nickname,
			@RequestParam(value = "sex", required = true) String sex
			){
		Tuser   user=new   Tuser();
		user.setHeadImg(headImg);
		user.setTuserNick(nickname);
		user.setTuserSex("1");
		user.setCreatedAt(new  Date());
		user.setDataStatus(new   Byte("1"));
		int  	 sfs =  	userService.userRegister(user);
		return 	sfs;
	}
	
	
	
	/*
	 * 微信登录并存储微信用户信息
	 * GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
	 * 用户昵称
	 * 头像链接
	 * 性别
	 * 
	 *  必须是在用户已经授权的情况下调用
		wx.getUserInfo({
		  success(res) {
		    const userInfo = res.userInfo
		    const nickName = userInfo.nickName
		    const avatarUrl = userInfo.avatarUrl
		    const gender = userInfo.gender // 性别 0：未知、1：男、2：女
		    const province = userInfo.province
		    const city = userInfo.city
		    const country = userInfo.country
		  }
		})
	 */
	@RequestMapping(value = "/wxLogin", method = RequestMethod.GET)
	public   JSONObject   wxLogin(@RequestParam(value = "js_code", required = true) String  js_code,@RequestParam(value = "nickName", required = true) String  nickName,
								  @RequestParam(value = "avatarUrl", required = true) String  avatarUrl, @RequestParam(value = "gender", required = true) String  gender,
								  @RequestParam(value = "province", required = true) String  province,@RequestParam(value = "city", required = true) String  city,
								  @RequestParam(value = "country", required = true) String  country
								  ){
		Tuser   tuser=new   Tuser();
		tuser.setCreatedAt(new  Date());
		tuser.setHeadImg(avatarUrl);
		tuser.setTuserNick(nickName);
		tuser.setTuserSex(gender);
		tuser.setDataStatus(new Byte("1"));
		
		UserInfo   userInfo=new   UserInfo();
		userInfo.setClocation(province+","+city+","+country);
		userInfo.setCreatedAt(new  Date());
		userInfo.setDataStatus(new Byte("1"));
		JSONObject  	 sfs =  	userService.wxLogin(js_code,userInfo,tuser);
		return 	sfs;
	}
	
	

}
